import threading


import time
from midwordKeysThread import midwordkeysThread
from dataPath import fenci_posseg_filepath,fenci_allfilepath


def  creatseekMidkeysThreads(keyWords, optimization):
    #########如果选择优化，就使用词性分词的数据集
    if optimization:
        file = open(fenci_posseg_filepath, 'rb')
        fencilog = file.readlines()
    else :
        file = open(fenci_allfilepath, 'rb')
        fencilog = file.readlines()
    start_time = time.time()
    threads = []
    for i, k in enumerate(keyWords, 1):
    #optimization传入使得线程处理数据是稍有不同,因为打开文件不同
        thread = midwordkeysThread(i, "Thread-" + str(i),k, fencilog, optimization)
        threads.append(thread)
        threads[i-1].start()
        #循环运行线程，并设置阻塞，就是要等子进程结束，才继续
    for i, k in enumerate(keyWords, 0):
        threads[i].join()
    file.close()
    end_time = time.time()
    num_seedkeys =[]
    for i, k in enumerate(keyWords, 0):
        num_seedkeys.append(threads[i].num_seedkeys)
        if optimization:
            print("一次分词终身受用----使用词性---中介关键字求取----执行时间为{:.4f}秒".format(end_time - start_time))
        else:
            print("一次分词终身受用----不使用词性----中介关键字求取----执行时间为{:.4f}秒".format(end_time - start_time))
    return num_seedkeys         
    



